/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.lucidhex.viewer;

import com.kitfox.svg.SVGCache;
import com.kitfox.svg.app.beans.SVGIcon;
import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.StringReader;
import java.net.URI;
import javax.swing.Icon;

/**
 *  
 * @author Lucid Wolf
 */
public class LucidIcon implements Icon{
    public static LucidIcon EMPTY = new LucidIcon();
    private SVGIcon icon = null;
    private String name = "EMPTY";
    private int radius = 16;
    public LucidIcon(){
        
    }
    public LucidIcon(String name, String xml) {
        this.name = name;
        loadSVGIcon(name, xml);
    }
    @Override
    public void paintIcon(Component cmpnt, Graphics grphcs, int x, int y) {
        if(icon == null){return;}
        icon.paintIcon(cmpnt, grphcs, x, y);
    }
    @Override
    public int getIconWidth() {
        return icon.getIconWidth();
    }
    @Override
    public int getIconHeight() {
        return icon.getIconHeight();
    }
    public final void loadSVGIcon(String name, String xml){
        this.name = name;
        StringReader reader = new StringReader(xml);
        URI uri = SVGCache.getSVGUniverse().loadSVG(reader, name); 
        icon = new SVGIcon();
        icon.setSvgURI(uri);  
        radius = (int)Math.floor(Math.sqrt(icon.getIconHeight()*icon.getIconHeight()+icon.getIconWidth()*icon.getIconWidth())/2.0);
    }
    public String getName(){
        return name;
    }
    public BufferedImage getRotatedImage(double scale, double rotation){
        scale = scale/radius;        
        double r1 = Math.abs( Math.cos(-rotation ) );
        double r2 = Math.abs( Math.sin(-rotation ) );        
        //int rotWidth = (int)Math.floor(scale*(icon.getIconWidth() * r1 + icon.getIconHeight() * r2));
        //int rotHeight = (int)Math.floor(scale*(icon.getIconHeight() * r1 + icon.getIconWidth() * r2));
        int rotWidth = (int)Math.floor(scale*icon.getIconWidth());
        int rotHeight = (int)Math.floor(scale*icon.getIconHeight());
        BufferedImage bi = new BufferedImage(rotWidth, rotHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D)bi.createGraphics();
        int cWidth = icon.getIconWidth() / 2;
        int cHeight = icon.getIconHeight() / 2;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform original = g2.getTransform();
        AffineTransform at = new AffineTransform();
        at.concatenate(original);
        //at.translate((getIconWidth() - icon.getIconWidth()) / 2, (getIconHeight() - icon.getIconHeight()) / 2);
        at.scale(scale, scale);
        at.rotate(-rotation, cWidth, cHeight);
        g2.setTransform(at);
        icon.paintIcon(null, g2, 0, 0);
        g2.setTransform(original);
        return bi;
    }
}
